/*
 Source: ufxTrader.java
 Software Copyright: Hundsun Technologies Inc.
 System: UFX Security Demo (Asynchronous)
 Module: Package input parameter(Report push related)
 Describition:
 This class is packing report publish related input parameters for each function and send the package to the broker, the documents for function 
 description and Java programming helper can be found at ufx website: https://ufx.hscloud.cn/ufx/html/doc/index.html
 Function id include: 620001-12
 author: Kai Zhan
 Date:2016/03/29
 Others:
 This demo is a sample for using Hundsun UFX System, we are highly not recommand any user to use this demo at any real market，for more details 
 please read <T2SDK JAVA Developer Manual>
 */
package demo.ufx.md;

import com.hundsun.t2sdk.interfaces.share.dataset.IDatasets;
import java.util.logging.Logger;
import com.hundsun.t2sdk.common.core.context.ContextUtil;
import com.hundsun.t2sdk.common.share.dataset.DatasetService;
import com.hundsun.t2sdk.common.share.event.PackService;
import com.hundsun.t2sdk.impl.client.T2Services;
import com.hundsun.t2sdk.interfaces.IClient;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;
import com.hundsun.t2sdk.interfaces.share.event.EventTagdef;
import com.hundsun.t2sdk.interfaces.share.event.EventType;
import com.hundsun.t2sdk.interfaces.share.event.IEvent;
import com.hundsun.t2sdk.interfaces.share.event.IPack;

public class ufxMD {

    private IClient client = null;
    private T2Services server = T2Services.getInstance();// 获取T2实例
    private Logger logger = Logger.getLogger("ufxMD");//用logger方式打印

    public void Init() throws Exception {
        //ar_tree_msg是配置在配置文件中的服务
        client = server.getClient("ar_tree_msg");//走节点ar_tree_msg
        mdCallBack.setClient(client);
    }
//订阅-行情实时信息（恒生环境不可用）

    public void ReqFunction620001_25(String fundAccount, String pwd, String futuCode) throws Exception {
        //打印功能号
        System.err.println("ReqFunction620001_25");
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("620001", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.appendRow();
        IPack outPack = PackService.getPacker(IPack.VERSION_2, "utf-8");//获取一个打包器
        outPack.addDataset(dataset);//数据打入包体
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 1);//分配节点为1，应答
        event.setIntegerAttributeValue(EventTagdef.TAG_ISSUE_TYPE, 25);//设定事件类型
        event.setByteArrayAttributeValue(EventTagdef.TAG_KEY_INFO, outPack.Pack()); //打包
        unPack(event, event.getServiceId(), event.getStringAttributeValue(EventTagdef.TAG_ISSUE_TYPE)); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包
    }
//成交主推

    public void ReqFunction620001_12(String account, String branch_no) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("620001", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("branch_no");//分支机构
        dataset.appendRow();
        dataset.updateString("fund_account", account);
        dataset.updateString("branch_no", branch_no);
        event.setServiceId("620001");//打入功能号
        IPack outPack = PackService.getPacker(IPack.VERSION_2, "utf-8");//获取一个打包器
        outPack.addDataset(dataset); //数据打入包体
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 1);//分配节点为1，应答
        event.setIntegerAttributeValue(EventTagdef.TAG_ISSUE_TYPE, 12);//打包事件类型
        event.setByteArrayAttributeValue(EventTagdef.TAG_KEY_INFO, outPack.Pack()); //打包
        unPack(event, event.getServiceId(), event.getStringAttributeValue(EventTagdef.TAG_ISSUE_TYPE)); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包

    }

    public void ReqFunction620001_23(String account, String branch_no) throws Exception {
        // 获取event
        IEvent event = ContextUtil.getServiceContext().getEventFactory()
                .getEventByAlias("620001", EventType.ET_REQUEST);
        // 往event中添加dataset
        IDataset dataset = DatasetService.getDefaultInstance().getDataset();
        dataset.addColumn("fund_account");//资产账户
        dataset.addColumn("branch_no");//分支机构
        dataset.appendRow();
        dataset.updateString("fund_account", account);
        dataset.updateString("branch_no", branch_no);
        event.setServiceId("620001");//打入功能号
        IPack outPack = PackService.getPacker(IPack.VERSION_2, "utf-8");//获取一个打包器
        outPack.addDataset(dataset); //数据打入包体
        event.setIntegerAttributeValue(EventTagdef.TAG_SENDERID, 1);//分配节点为1，应答
        event.setIntegerAttributeValue(EventTagdef.TAG_ISSUE_TYPE, 23);//打包事件类型
        event.setByteArrayAttributeValue(EventTagdef.TAG_KEY_INFO, outPack.Pack()); //打包
        unPack(event, event.getServiceId(), event.getStringAttributeValue(EventTagdef.TAG_ISSUE_TYPE)); //入参解包，同时打印发送包参数信息
        client.send(event);//发送包

    }

    //主推包解包，并打印包
    public void unPack(IEvent event, String functionId, String issueType) {
        // 获取过滤信息
        //相当于IBizMessage的GetKeyInfo
        byte[] keyInfo = event.getByteArrayAttributeValue(EventTagdef.TAG_KEY_INFO);
        IPack outPack = PackService.getPacker(keyInfo, "utf-8");
        StringBuilder sb = new StringBuilder();
        // 获得结果集
        // 开始读取单个结果集的信息
        IDataset ds = outPack.getDataset(0);//获取数据包
        int columnCount = ds.getColumnCount();//获取
        // 遍历单个结果集列信息
        for (int j = 1; j <= columnCount; j++) {
            sb.append(ds.getColumnName(j));//打印数据标题
            sb.append("|");
            //sb.append(ds.getColumnType(j)); //打印数据类型
            //sb.append("\n");
        }
        // 遍历单个结果集记录，遍历前首先将指针置到开始
        ds.beforeFirst();
        while (ds.hasNext()) {
            sb.append("\n");
            ds.next();
            for (int j = 1; j <= columnCount; j++) {
                sb.append(ds.getString(j));//打印数据集
                sb.append("|");
            }
        }
        System.out.println("功能号" + functionId + "，事件类型" + issueType + "，成交主推回报\n" + "订阅成功" + "以下是相关入参：\n" + sb);
    }
}
